clear 
local obs = 1000
local df = `obs' - 2
local alpha = 0.05
local value1 = -0.3
local value2 = 0.3
set obs `obs'
gen treat = 0
replace treat = 1 if _n > `obs'/2
set seed 1000

// In the examples below, values less than 0.2 in absolute value are determined in 
// advance to be economically unimportant. Values larger than 0.2 in absolute value
// are considered to be economically important.

*****************************
********  EXAMPLE 1 *********
*****************************;

local beta = 0.1
local sigma = 1
gen y = `beta'*treat + rnormal(0,`sigma')
regress y treat
ttest y, by(treat)

scalar diff = r(mu_2) - r(mu_1)
scalar sd = sqrt((r(sd_1)^2 + r(sd_2)^2)/2)
scalar effect = diff/sd
scalar list effect diff sd


scalar sdeffect = r(se)
scalar crit1 = `value1' - invt(`df',1-`alpha'/2)*sdeffect
scalar crit2 = `value2' + invt(`df',1-`alpha'/2)*sdeffect
// MINIMUM EFFECT TEST: -0.3 <= effect <= 0.3
scalar list effect crit1
scalar list effect crit2
// Reject H01 if: effect < crit1
// Reject H02 if: effect > crit2


scalar crit1 = `value1' + invt(`df',1-`alpha')*sdeffect
scalar crit2 =  `value2' - invt(`df',1-`alpha')*sdeffect
// EQUIVALENCE TEST: -0.3 < effect < 0.3
scalar list effect crit1
scalar list effect crit2
// Reject H01 if: effect > crit1
// Reject H02 if: effect < crit2

*****************************
********  EXAMPLE 2 *********
*****************************;

set seed 1000
local beta = 0.25
local sigma = 1
replace y = `beta'*treat + rnormal(0,`sigma')
regress y treat
ttest y, by(treat)

scalar diff = r(mu_2) - r(mu_1)
scalar sd = sqrt((r(sd_1)^2 + r(sd_2)^2)/2)
scalar effect = diff/sd
scalar list effect diff sd


scalar sdeffect = r(se)
scalar crit1 = `value1' - invt(`df',1-`alpha'/2)*sdeffect
scalar crit2 = `value2' + invt(`df',1-`alpha'/2)*sdeffect
// MINIMUM EFFECT TEST: -0.3 <= effect <= 0.3
scalar list effect crit1
scalar list effect crit2
// Reject H01 if: effect < crit1
// Reject H02 if: effect > crit2


scalar crit1 = `value1' + invt(`df',1-`alpha')*sdeffect
scalar crit2 = `value2' - invt(`df',1-`alpha')*sdeffect
// EQUIVALENCE TEST: -0.3 < effect < 0.3
scalar list effect crit1
scalar list effect crit2
// Reject H01 if: effect > crit1
// Reject H02 if: effect < crit2
